import axios, { AxiosError, AxiosInstance, InternalAxiosRequestConfig, AxiosResponse } from 'axios'
import { ElMessage } from 'element-plus'
// 创建axios实例
const service: AxiosInstance = axios.create({
    baseURL: 'http://localhost:8081',
    timeout: 60000
})
// axios 请求拦截
service.interceptors.request.use(
    (config: InternalAxiosRequestConfig) => {
        return config
    },
    (error: AxiosError) => {
        ElMessage.error(error)
        Promise.reject(error)
    }
)
// axios 响应拦截
service.interceptors.response.use(
    (response: AxiosResponse<any>) => {
        const { data } = response
        if (data.code !== 200 && data.code !== 0) {
            throw new Error(data.msg || data.message || 'error')
        } else {
            return data
        }
    },
    error => {
        return Promise.reject(error)
    }
)
export default service
